# coding=utf-8
"""
2019-3-25 by Stone at GDOU-KC
"""
from flask import request
from flask import redirect
from flask import Response

from Handler import tags
from Handler import ExpireException
import person


def interface_sign_up():
    tag = request.args.get('tag')
    print('tag: ', tag)

    # skip over if expired
    try:
        tags.check_expire(tag)
    except ExpireException:
        return redirect(r'/static/expire.html')

    # save information if not expired
    resp = 'OK'
    if request.method == 'GET':
        with open(r'sign_up.html', 'r', encoding='utf-8') as f:
            resp = ''.join(f.readlines())
            resp = resp.format(tag=tag)  # tag for action of form

    elif request.method == 'POST':
        info = request.form

        # p = person.create_a_person(**info)
        # p.open_id = tag
        # p.save()
        info = dict()
        for k in request.form:
            info[k] = request.form[k]

        open_id = tags.get_data(tag)['open_id']
        person.add_a_person(open_id=open_id, **info)

        resp = redirect(r'static/success.html')

    return resp


